package com.neusoft.servlet;

import com.alibaba.fastjson.JSON;
import com.neusoft.entity.TagsEntity;
import com.neusoft.mapper.TagMapper;
import com.neusoft.utils.SqlSessionFactoryUtils;
import com.neusoft.utils.UserUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/tags")
public class TagServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String loginUserId = UserUtils.getLoginUserId().toString();
        System.out.println("TagServlet threadid: "+Thread.currentThread().getId()+"userid:" + loginUserId);
        SqlSessionFactory sqlSessionFactory
                = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();

        TagMapper tagMapper = sqlSession.getMapper(TagMapper.class);
        List<TagsEntity> allTags = tagMapper.getAllTags();

        sqlSession.close();
        String jsonObject = JSON.toJSONString(allTags);
        PrintWriter printWriter = resp.getWriter();
        printWriter.println(jsonObject);
    }
}
